package com.zuo.mall.service.impl;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.dto.PmsProductAttributeParam;
import com.zuo.mall.dto.ProductAttrInfo;
import com.zuo.mall.entity.PmsProductAttribute;
import com.zuo.mall.entity.PmsProductAttributeCategory;
import com.zuo.mall.mapper.PmsProductAttributeMapper;
import com.zuo.mall.service.PmsProductAttributeCategoryService;
import com.zuo.mall.service.PmsProductAttributeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PmsProductAttributeServiceImpl extends ServiceImpl<PmsProductAttributeMapper, PmsProductAttribute> implements PmsProductAttributeService {

    @Autowired
    private PmsProductAttributeCategoryService productAttributeCategoryService;

    @Override
    public PmsProductAttribute getItem(Long attributeId) {
        PmsProductAttribute productAttribute = baseMapper.selectById(attributeId);
        return productAttribute;
    }

    @Override
    public List<ProductAttrInfo> getProductAttrInfo(Long productCategoryId) {
        return baseMapper.getProductAttrInfo(productCategoryId);
    }

    @Override
    public CommonPage<PmsProductAttribute> listByAttributeCategoryId(Long attributeCategoryId, Integer pageNum, Integer pageSize, Integer type) {
        LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId, attributeCategoryId);
        queryWrapper.eq(PmsProductAttribute::getType, type);

        Integer total = baseMapper.selectCount(queryWrapper);
        queryWrapper.last("LIMIT " + PageUtil.getStart(pageNum - 1, pageSize) + "," + pageSize);
        List<PmsProductAttribute> pmsProductAttributes = baseMapper.selectList(queryWrapper);
        CommonPage<PmsProductAttribute> commonPage = CommonPage.restPage(pmsProductAttributes, Long.valueOf(total), pageSize);
        return commonPage;
    }

    @Override
    public int create(PmsProductAttributeParam pmsProductAttributeParam) {
        PmsProductAttribute productAttribute = new PmsProductAttribute();
        BeanUtils.copyProperties(pmsProductAttributeParam, productAttribute);

        int insert = baseMapper.insert(productAttribute);
        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(pmsProductAttributeParam.getProductAttributeCategoryId());

        // 添加商品属性后更新商品属性分类数量
        if (productAttribute.getType() == 0) {
            // 增加规格数量
            productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount() + 1);
        }else if (productAttribute.getType() == 1) {
            productAttributeCategory.setParamCount(productAttributeCategory.getParamCount() + 1);
            // 增加参数数量
        }
        productAttributeCategoryService.updateById(productAttributeCategory);
        return insert;
    }

    @Override
    public int update(Long attributeId, PmsProductAttributeParam productAttributeParam) {
        PmsProductAttribute productAttribute = new PmsProductAttribute();
        BeanUtils.copyProperties(productAttributeParam, productAttribute);
        productAttribute.setId(attributeId);
        int i = baseMapper.updateById(productAttribute);
        return i;
    }

    @Transactional
    @Override
    public int delete(List<Long> ids) {
        // 获取分类
        Long id = ids.get(0);
        PmsProductAttribute productAttribute = baseMapper.selectById(id);
        Integer type = productAttribute.getType();
        Long productAttributeCategoryId = productAttribute.getProductAttributeCategoryId();
        int i = baseMapper.deleteBatchIds(ids);
        // 查询productAttributeCategory
        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryService.getById(productAttributeCategoryId);
        if (type == 1) {
            // 减少参数
            productAttributeCategory.setParamCount(productAttributeCategory.getParamCount() - 1);
        }else if (type == 0) {
            // 减少规格
            productAttributeCategory.setAttributeCount(productAttributeCategory.getAttributeCount() -1);
        }

        productAttributeCategoryService.updateById(productAttributeCategory);
        return i;
    }

    @Override
    public int deleteListByProductAttributeCategoryId(Long id) {
        LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId, id);
        int delete = baseMapper.delete(queryWrapper);
        return delete;
    }
}
